module BinaryToBCD(
    input [11:0] binary,
    output reg [3:0] h,
    output reg [3:0] t,
    output reg [3:0] u
);
    integer i;
    reg [19:0] shift;

    always @(*) begin
        shift = 0;
        shift[11:0] = binary;
        h = 0; t = 0; u = 0;
        for(i=0; i<12; i=i+1) begin
            if(h >=5) h = h +3;
            if(t >= 5) t = t +3;
            if(u >=5) u = u +3;
            {h, t, u, shift} = {h, t, u, shift} << 1;
        end
    end
endmodule

module BinaryToBCD_8bit(
    input [7:0] binary,
    output reg [3:0] high,
    output reg [3:0] low
);
    integer i;
    reg [11:0] shift;

    always @(*) begin
        shift = 0;
        shift[7:0] = binary;
        high = 0; low = 0;
        for(i=0; i<8; i=i+1) begin
            if(high >=5) high = high +3;
            if(low >=5) low = low +3;
            {high, low, shift} = {high, low, shift} <<1;
        end
    end
endmodule
